package client

import (
	"net/rpc"

	"aqielife.cn/spider/singleSpider/zhenaiDistributed/config"
	"aqielife.cn/spider/singleSpider/zhenaiDistributed/engine"
	"aqielife.cn/spider/singleSpider/zhenaiDistributed/worker"
)

func CreateProcessor(clientChan chan *rpc.Client) (engine.Processor, error) {
	return func(request engine.Request) (engine.ParseResult, error) {

		sReq := worker.SerializeRequest(request)
		var sResult worker.ParseResult
		client := <-clientChan
		err := client.Call(config.CrawlServiceRpc, sReq, &sResult)
		if err != nil {
			return engine.ParseResult{}, err
		}

		return worker.DeserializeResult(sResult), nil
	}, nil
}
